import type { ServiceAPIURI, ServiceResult } from "../api/config.api.service";

type CountryConfig = ServiceResult<ServiceAPIURI.ConfCoun>;
type OtherConfig = ServiceResult<ServiceAPIURI.ConfOther>;

export default class ConfManager {
	private static coun_code: string;
	private static lang_code: string;

	private static coun_conf: CountryConfig;
	private static othr_conf: OtherConfig;

	private static master_list: Array<mtec.Fusion<{remove_total: number}, OtherConfig['masterPassConfig'][number]>>

	/** 当前的国家代码 */
	public static get country(){
		return ConfManager.coun_code;
	}
	/** 当前的语言代码 */
	public static get language(){
		return ConfManager.lang_code;
	}
	/** 当前的国家配置 */
	public static get countryConf(){
		return ConfManager.coun_conf;
	}
	/** 当前项目的其他配置 */
	public static get otherConf(){
		return ConfManager.othr_conf;
	}
	/** 获取通行证配置列表 */
	public static get masterList(){
		return ConfManager.master_list;
	}

	/** 国家代码修正 */
	public static safeCountry(country: string|number){
		if(typeof country=='number'){
			country = COUN_LIST[country];
		}

		if(!COUN_LIST.includes(country)){
			country = COUN_LIST[0];
		}

		return country;
	}

	/** 设置项目的国家配置 */
	public static setCountryConfig(conf: CountryConfig){
		ConfManager.coun_code = conf.country;
		ConfManager.lang_code = conf.language;
		ConfManager.coun_conf = conf;
	}

	/** 设置项目的其他配置 */
	public static setOtherConfig(conf: OtherConfig){
		ConfManager.othr_conf = conf;

		ConfManager.master_list = conf.masterPassConfig.map((el, idx, ls)=>{

			if(idx==0){
				Reflect.set(el, 'remove_total', el.eliminateCount);
			}else{
				Reflect.set(el, 'remove_total', el.eliminateCount+Reflect.get(ls[idx-1], 'remove_total'));
			}

			return el as any;
		});

		ConfManager.master_list.sort((a, b)=>a.level-b.level);
	}
}

/** 当前项目支持的国家配置 */
const COUN_LIST = ['US', 'CA', 'AU'];